﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace zmMoniter
{
    public partial class frmNavWarnHistory : zmMoniter.Basefrm
    {
        private mainForm main = null;
        struct  CmbInfo
        {
            public int _key; public string _text;
            public CmbInfo(int key, string text)
            {
                _key = key; _text = text;
            }
            public override string ToString()
            {
                return _text;
            }
        }
        private string sSQL="";
        public frmNavWarnHistory(mainForm frm)
        {
            InitializeComponent();
            this.main = frm;
            sSQL = "";
        }
        private void selectDam(string damName)
        {
            cmbox_damBlock.Properties.Items.Clear();
            cmbox_damBlock.Text = (string)cmbox_damBlock.Tag;

            if (damName == "上坝块")
            {
                for (int i = 1; i < 20; i++)
                {
                    cmbox_damBlock.Properties.Items.Add(new CmbInfo(i, "坝段" + i.ToString()));
                }
            }
            else if (damName == "下坝块")
            {
                for (int i = 2; i < 19; i++)
                {
                    cmbox_damBlock.Properties.Items.Add(new CmbInfo(i, "坝段" + i.ToString()));
                }
            }
        }
        private void cmbox_dam_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectDam(cmbox_dam.EditValue.ToString());

        }
        private void selectDamBlock(string sDam, string sDamBlock)
        {
            cmbox_deck.Properties.Items.Clear();
            cmbox_deck.Text = (string)cmbox_deck.Tag;

            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string sSQL = "";
            if (sDam == "上坝块")
                sSQL = "SELECT * FROM 仓面信息 WHERE 上下游块='S' AND 坝段=" + sDamBlock;
            else if (sDam == "下坝块")
                sSQL = "SELECT * FROM 仓面信息 WHERE 上下游块='X' AND 坝段=" + sDamBlock;
            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();
            if (dt == null)//modify by lance 2013-1-20
                return;
            int iCount = dt.Rows.Count;
            for (int i = 0; i < iCount; i++)
            {
                DataRow dataRow = dt.Rows[i];
                int iIDSQL = int.Parse(dataRow["编号"].ToString().Trim());
                string sNameSQL = dataRow["仓面名称"].ToString().Trim();
                //string sMinHeight = dataRow["最低高程"].ToString().Trim();
                //string sMaxHeight = dataRow["最高高程"].ToString().Trim();
                // string sVibRange = dataRow["振捣边界点"].ToString().Trim();
                // string sDeckRange = dataRow["仓面控制点"].ToString().Trim();
                cmbox_deck.Properties.Items.Add(new CmbInfo(iIDSQL, sNameSQL.ToString()));

            }
        }
        private void cmbox_damBlock_SelectedIndexChanged(object sender, EventArgs e)
        {
            CmbInfo p = new CmbInfo();
            p = (CmbInfo)(cmbox_damBlock.SelectedItem);
            int value = p._key;
            string damName = cmbox_dam.EditValue.ToString();
            selectDamBlock(damName, value.ToString());
        }
        private void selectDeck(int iDeckID)
        {

            cbxEdt_Layer.Properties.Items.Clear();
            cbxEdt_Layer.Text = (string)cbxEdt_Layer.Tag;

            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string sSQL = "";

            sSQL = "SELECT * FROM 铺层信息表 WHERE 仓面号=" + iDeckID + " Order by 层编号";

            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();
            if (dt == null)//modify by lance 2013-1-20
                return;
            int iCount = dt.Rows.Count;
            for (int i = 0; i < iCount; i++)
            {
                DataRow dataRow = dt.Rows[i];
                int iIDSQL = int.Parse(dataRow["编号"].ToString().Trim());
                string sNameSQL = dataRow["层编号"].ToString().Trim();

                cbxEdt_Layer.Properties.Items.Add(new CmbInfo(iIDSQL, sNameSQL.ToString()));

            }

        }
        private void cmbox_deck_SelectedIndexChanged(object sender, EventArgs e)
        {
            CmbInfo p = (CmbInfo)(cmbox_deck.SelectedItem);
            selectDeck(p._key);
        }
        private void cbxEdt_Layer_SelectedIndexChanged(object sender, EventArgs e)
        {
           
                
            
        }
        private void pictureBox_Btn_Click(object sender, EventArgs e)
        {
            label_msg.Text = "正在查找中...";
            label_msg.Visible = true;
            if (cbxEdt_Layer.Text != "请选择铺层")
            {
                CmbInfo p = (CmbInfo)(cbxEdt_Layer.SelectedItem);
                int iLayerID = p._key;
                p = (CmbInfo)(cmbox_deck.SelectedItem);
                int iDeckID = p._key;
               
                sSQL = "select 预警表.GUID AS GUID," +
                    "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
                    "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间,to_char(预警表.处理时间,'yyyy-mm-dd hh24:mi:ss') AS 处理时间," +
                    "预警表.X,预警表.Y,预警表.处理详情,仓面信息.仓面名称,预警表.仓面号,预警处理状态.名称 AS 处理状态,用户.用户名 " +
                    "FROM 预警表 LEFT OUTER JOIN " +
                    "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 "+
                    "LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号 " +
                    "LEFT OUTER JOIN 用户 ON 预警表.处理人 = 用户.ID " +
                    "LEFT OUTER JOIN 预警处理状态 ON 预警表.处理状态 = 预警处理状态.编号 " +
                    "WHERE 预警表.仓面号=" + iDeckID + " AND 预警表.铺层=" + iLayerID;
                if (dateEdt_Start.Text != "")
                {
                    sSQL += " AND 报警时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                }
                if (dateEdt_End.Text != "")
                {
                    sSQL += " AND 报警时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                }
                sSQL += " Order by 预警时间";
            }
            else if (cmbox_deck.Text != "请选择仓面")
            {
                CmbInfo p = (CmbInfo)(cmbox_deck.SelectedItem);
                int iDeckID = p._key;
                sSQL = "select 预警表.GUID AS GUID," +
                    "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
                    "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间,to_char(预警表.处理时间,'yyyy-mm-dd hh24:mi:ss') AS 处理时间," +
                    "预警表.X,预警表.Y,预警表.处理详情,仓面信息.仓面名称,预警表.仓面号,预警处理状态.名称 AS 处理状态,用户.用户名 " +
                    "FROM 预警表 LEFT OUTER JOIN " +
                    "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 "+
                    "LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号 " +
                     "LEFT OUTER JOIN 用户 ON 预警表.处理人 = 用户.ID " +
                                         "LEFT OUTER JOIN 预警处理状态 ON 预警表.处理状态 = 预警处理状态.编号 " +
                    "WHERE 预警表.仓面号=" + iDeckID;
                if (dateEdt_Start.Text != "")
                {
                    sSQL += " AND 报警时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                }
                if (dateEdt_End.Text != "")
                {
                    sSQL += " AND 报警时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                }
                sSQL += " Order by 预警时间";
            }
            else if (cmbox_damBlock.Text != "请选择坝段")
            {
                CmbInfo p = (CmbInfo)(cmbox_damBlock.SelectedItem);
                int iDamBlockID = p._key;
                string damName = cmbox_dam.EditValue.ToString();
                if (damName == "上坝块")
                {
                    damName = "S";
                }
                else if (damName == "下坝块")
                {
                    damName = "X";
                }
                sSQL = "select 预警表.GUID AS GUID," +
                    "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
                    "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间,to_char(预警表.处理时间,'yyyy-mm-dd hh24:mi:ss') AS 处理时间," +
                    "预警表.X,预警表.Y,预警表.处理详情,仓面信息.仓面名称,预警表.仓面号,预警处理状态.名称 AS 处理状态,用户.用户名 " +
                    "FROM 预警表 LEFT OUTER JOIN " +
                    "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 "+
                    "LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号 " +
                    "LEFT OUTER JOIN 用户 ON 预警表.处理人 = 用户.ID " +
                    "LEFT OUTER JOIN 预警处理状态 ON 预警表.处理状态 = 预警处理状态.编号 " +
                    "WHERE 仓面信息.坝段=" + iDamBlockID + " AND 仓面信息.上下游块='"+damName+"'";
                if (dateEdt_Start.Text != "")
                {
                    sSQL += " AND 报警时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                }
                if (dateEdt_End.Text != "")
                {
                    sSQL += " AND 报警时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                }
                sSQL += " Order by 预警时间";
            }
            else if (cmbox_dam.Text != "请选择坝块")
            {

                string damName = cmbox_dam.EditValue.ToString();
                if (damName == "上坝块")
                {
                    damName = "S";
                }
                else if (damName == "下坝块")
                {
                    damName = "X";
                }
                sSQL = "select 预警表.GUID AS GUID," +
                    "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
                    "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间,to_char(预警表.处理时间,'yyyy-mm-dd hh24:mi:ss') AS 处理时间," +
                    "预警表.X,预警表.Y,预警表.处理详情,仓面信息.仓面名称,预警表.仓面号,预警处理状态.名称 AS 处理状态,用户.用户名 " +
                    "FROM 预警表 LEFT OUTER JOIN " +
                    "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 " +
                    "LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号 " +
                    "LEFT OUTER JOIN 用户 ON 预警表.处理人 = 用户.ID " +
                    "LEFT OUTER JOIN 预警处理状态 ON 预警表.处理状态 = 预警处理状态.编号 " +
                    "WHERE 仓面信息.上下游块='" + damName + "'";
                if (dateEdt_Start.Text != "")
                {
                    sSQL += " AND 报警时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                }
                if (dateEdt_End.Text != "")
                {
                    sSQL += " AND 报警时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                }
                sSQL += " Order by 预警时间 DESC";
            }
            else
            {
                sSQL = "select 预警表.GUID AS GUID," +
                    "振捣设备基本信息表.型号 AS 设备编号,预警表.铺层 AS 铺层编号," +
                    "预警表.备注,to_char(预警表.报警时间,'yyyy-mm-dd hh24:mi:ss') AS 预警时间,to_char(预警表.处理时间,'yyyy-mm-dd hh24:mi:ss') AS 处理时间," +
                    "预警表.X,预警表.Y,预警表.处理详情,仓面信息.仓面名称,预警表.仓面号,预警处理状态.名称 AS 处理状态,用户.用户名 " +
                    "FROM 预警表 LEFT OUTER JOIN " +
                    "仓面信息 ON 预警表.仓面号 = 仓面信息.编号 " +
                    "LEFT OUTER JOIN 用户 ON 预警表.处理人 = 用户.ID " +
                    "LEFT OUTER JOIN 预警处理状态 ON 预警表.处理状态 = 预警处理状态.编号 " +
                    "LEFT OUTER JOIN 振捣设备基本信息表 ON 预警表.设备编号 = 振捣设备基本信息表.编号";
                if (dateEdt_Start.Text != ""&&dateEdt_End.Text != "")
                    sSQL += " WHERE 报警时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')"+
                        " AND 报警时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                else if (dateEdt_Start.Text == ""&&dateEdt_End.Text != "")
                    sSQL += " WHERE 报警时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                else if (dateEdt_Start.Text != "" && dateEdt_End.Text== "")
                    sSQL += " WHERE 报警时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                sSQL += " Order by 预警时间 DESC";
            }
            if (sSQL == "" || main.frHistory == null)
            {
                label_msg.Text = "结果：查找出错！";
                label_msg.Visible = true;
                return;
            }
            sSQL = "select rownum AS 编号,t.* from(" + sSQL + ") t";
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            DataTable dt = db.ReturnDataTable(sSQL);
            if (dt == null)//modify by lance 2013-1-20
            {//modify by lance 2013-1-20
                label_msg.Text = "连接数据库失败!";//modify by lance 2013-1-20
                return;//modify by lance 2013-1-20
            }//modify by lance 2013-1-20
            int iCount = dt.Rows.Count;
            label_msg.Text = "结果：共查找到 " + iCount + " 条记录";
            label_msg.Visible = true;
            db.CloseConn();
            main.frHistory.gridCtl_WarnHistory.DataSource = dt;
            main.frmManger("Warning");

            if (main.barBtn_DrawDeckPlg.Enabled ==true)//在报警历史记录浏览的时候，禁用仓面编辑工具
                main.barBtn_DrawDeckPlg.Enabled = false;
        }
    }
}
